﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using Ocmf.Forms;

namespace Ocmf.Types
{
    public class Card : Action
    {
        public string CardKey
        {
            get
            {
                return ImageKey;
            }
            set
            {
                ImageKey = SelectedImageKey = value;
                Text = "Card: " + value;
            }
        }

        public Card()
        {
            //sets the icon shown in the treeview
            Text = "Card: Default Image";
            SelectedImageKey = ImageKey = "card";
        }

        public override object Clone()
        {
            var clone = base.Clone() as Card;
            clone.ImageKey = ImageKey;
            return clone;
        }

        public override Form Editor
        {
            get { return new EditImageActionForm(this);}
        }

        public override XmlElement ToXml(XmlDocument xmlDocument)
        {
            //create trial xml element
            var cardElement = xmlDocument.CreateElement("Card");
            //create, set and append text attribute
            var cardTextElement = xmlDocument.CreateAttribute("Text");
            cardTextElement.InnerText = Text;
            cardElement.Attributes.Append(cardTextElement);
            //create,set and append image attribute
            var imageKeyAttribute = xmlDocument.CreateAttribute("ImageKey");
            imageKeyAttribute.InnerText = ImageKey;
            cardElement.Attributes.Append(imageKeyAttribute);
            //iterate over each action
            foreach (Action action in Nodes)
                cardElement.AppendChild(action.ToXml(xmlDocument));
            //return element
            return cardElement;
        }

        public override void FromXml(XmlNode xmlElement)
        {
            Text = xmlElement.Attributes["Text"].Value;
            CardKey = xmlElement.Attributes["ImageKey"].Value;
            foreach (XmlNode xmlNode in xmlElement.ChildNodes)
            {
                Action action = null;
                switch (xmlNode.Name)
                {
                    case "Buzzer":
                        action = new Buzzer();
                        break;
                    case "Food":
                        action = new Food();
                        break;
                    case "GoTo":
                        action = new GoTo();
                        break;
                    case "HouseLight":
                        action = new HouseLight();
                        break;
                    case "Prl":
                        action = new Prl();
                        break;
                    case "Stop":
                        action = new Stop();
                        break;
                    case "Delay":
                        action = new Delay();
                        break;
                    case "Counter":
                        action = new Counter();
                        break;
                }

                action.FromXml(xmlNode);
                Nodes.Add(action);
            }
        }
    }
}
